﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace FySystem.Secure
{
    public class MD5
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string source)
        {
            string res = source;
            try
            {
                var md5 = System.Security.Cryptography.MD5.Create();
                byte[] bytes = Encoding.UTF8.GetBytes(source);
                res = BitConverter.ToString(md5.ComputeHash(bytes));
                res = res.Replace("-", "");
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        /// <summary>
        /// MD5加密返回字节流
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static byte[] MD5EncryptB(string source)
        {
            byte[] bytes = null;
            try
            {
                var md5 = System.Security.Cryptography.MD5.Create();
                bytes = Encoding.UTF8.GetBytes(source);
                bytes = md5.ComputeHash(bytes);
            }
            catch (Exception)
            {
                throw;
            }
            return bytes;
        }
    }
}
